﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.IO;

namespace Kiosk.WebManagement.js.tiny_mce.plugins.kioskImgLib
{
    public partial class Library : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadFiles();
        }

        private void LoadFiles()
        {
            List<string> temp = new List<string>(Directory.GetFiles(Server.MapPath("~/upload")));
            var files = from f in temp
                        where IsImage(f)
                        select new
                        {
                            FileName = f.Substring(f.LastIndexOf("\\") + 1, f.Length - 1 - f.LastIndexOf("\\"))
                        };
            grdFiles.DataSource = files;
            grdFiles.DataBind();
        }

        private bool IsImage(string fileName)
        {
            bool isImage = false;

            try
            {
                string ext = fileName.Split('.')[1].ToLower();
                string[] extensions = { "png", "gif", "bmp", "jpg", "jpeg" };
                var result = from s in extensions
                             where s.Contains(ext)
                             select s;
                isImage = result.Count() > 0;
            }
            catch
            {
                isImage = false;
            }
            return isImage;
        }
    }
}
